/**   
* @Package com.app.open.sys.permission.service 
*/
package com.wlyy.bcwlw.sys.permission.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.wlyy.bcwlw.common.cache.CacheEvict;
import com.wlyy.bcwlw.common.tree.BaseStaticTreeNode;
import com.wlyy.bcwlw.common.tree.StaticTree;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.application.dao.ApplicationDAO;
import com.wlyy.bcwlw.sys.application.entity.ApplicationDTO;
import com.wlyy.bcwlw.sys.config.ConfigConstant;
import com.wlyy.bcwlw.sys.menu.dao.ModuleDAO;
import com.wlyy.bcwlw.sys.menu.entity.ModuleDTO;
import com.wlyy.bcwlw.sys.menu.service.MenuService;
import com.wlyy.bcwlw.sys.permission.dao.ModulePermissionDAO;
import com.wlyy.bcwlw.sys.permission.dao.PermissionDAO;
import com.wlyy.bcwlw.sys.permission.dao.PermissionMenuDAO;
import com.wlyy.bcwlw.sys.permission.entity.ModulePermissionDTO;
import com.wlyy.bcwlw.sys.permission.entity.PermissionDTO;
import com.wlyy.bcwlw.sys.permission.entity.PermissionMenuDTO;
import com.wlyy.bcwlw.sys.role.dao.RolePermissionDAO;
import com.wlyy.bcwlw.sys.role.entity.RolePermissionDTO;

/**
 * @ClassName. PermissionService
 * @Description. TODO
 * @author zhangyang
 * @date 2015年8月10日 下午5:23:05
 * @version V1.0
 */
@Transactional
@Service
public class PermissionService {
	
	@Autowired
	private PermissionDAO permissionDAO;
	@Autowired
	private ModulePermissionDAO modulePermissionDAO;
	@Autowired
	private ApplicationDAO applicationDAO;
	@Autowired
	private PermissionMenuDAO permissionMenuDAO;
	@Autowired
	private RolePermissionDAO rolePermissionDAO;
	@Autowired
	private ModuleDAO moduleDAO;
	@Autowired
	private MenuService menuService;

	
	
	public TreeNode getAllPermissionTreeById(){
		
		List<BaseStaticTreeNode> nodeList = new ArrayList<BaseStaticTreeNode>();
		
		nodeList.add(new BaseStaticTreeNode(ConfigConstant.VirtualBaseTree, "系统权限树", null, "base"));
		List<ApplicationDTO> appList = applicationDAO.queryByObject(null);
		if(appList !=null && appList.size() !=0){
			for(ApplicationDTO app:appList){
				nodeList.add(new BaseStaticTreeNode(app.getAppId(), app.getAppName(), ConfigConstant.VirtualBaseTree, "app"));
			}
		}
		
		List<ModuleDTO> sortModuleList = moduleDAO.queryListByCriteria(null, "sort asc", null, null);
		List<String> moduleIdList = new ArrayList<String>();
		for(ModuleDTO module:sortModuleList){
			BaseStaticTreeNode treeNode = new BaseStaticTreeNode(module.getId(), module.getName(), module.getAppId(), "module");
			nodeList.add(treeNode);
			moduleIdList.add(module.getId());
		}
		if(moduleIdList.size() !=0){
			List<PermissionDTO> permissionList = permissionDAO.getChildPermissionByModuleId(moduleIdList);
			for(PermissionDTO permission:permissionList){
				BaseStaticTreeNode treeNode = new BaseStaticTreeNode(permission.getId(), permission.getName(), permission.getPermissionModuleId(), "permission");
				nodeList.add(treeNode);
			}
		}
		
		StaticTree tree = new StaticTree(nodeList.toArray(new BaseStaticTreeNode[0]),ConfigConstant.VirtualBaseTree);
		tree.render();
		return tree.getRootNode();
		
		
	}
	
	@CacheEvict(key="userPermissionCache")
	public PermissionDTO  savePermissionInfo(PermissionDTO permissionDTO,String moduleId){
		if(StringUtils.isEmpty(moduleId)){
			return null;
		}
		if(StringUtils.isEmpty(permissionDTO.getId())){
			permissionDAO.create(permissionDTO);
			ModulePermissionDTO insertModulePermissionDTO = new ModulePermissionDTO();
			insertModulePermissionDTO.setModuleId(moduleId);
			insertModulePermissionDTO.setPermissionId(permissionDTO.getId());
			modulePermissionDAO.create(insertModulePermissionDTO);
		}else{
			PermissionDTO query  = permissionDAO.findById(permissionDTO.getId());
			if(query == null){
				return null;
			}
			permissionDAO.update(permissionDTO);
		}
		return permissionDTO;
	}
	
	@CacheEvict(key="userPermissionCache")
	public boolean removePermissionById(String permissionId){
		if(StringUtils.isEmpty(permissionId)){
			return false;
		}
		//删除权限-菜单
		ModulePermissionDTO queryModulePermissionDTO = new ModulePermissionDTO();
		queryModulePermissionDTO.setPermissionId(permissionId);
		modulePermissionDAO.remove(queryModulePermissionDTO);
		//删除权限-角色
		RolePermissionDTO queryRolePermissionDTO = new RolePermissionDTO();
		queryRolePermissionDTO.setPermissionId(permissionId);
		rolePermissionDAO.remove(queryRolePermissionDTO);
		//删除权限
		permissionDAO.removeById(permissionId);
		return true;
		
	}
	
	public Map<String,Object> showPermissionOwnMenuTree(String permissionId){
		if(StringUtils.isEmpty(permissionId)){
			return null;
		}
		Map<String,Object> retMap = new HashMap<String,Object>();
		TreeNode menuTree = menuService.getMenuTree(null, true);
		List<String> ownMenus = permissionDAO.findPermissionOwnMenusById(permissionId);
		retMap.put("menuTree", menuTree);
		retMap.put("ownMenus", ownMenus);
		return retMap;
		
		
	}
	
	@CacheEvict(key="userPermissionCache")
	public boolean savePermissionOwnMenu(String permissionId,String[] ownMenus){
		if(StringUtils.isEmpty(permissionId)){
			return false;
		}
		PermissionMenuDTO queryPermissionMenuDTO = new PermissionMenuDTO();
		queryPermissionMenuDTO.setPermissionId(permissionId);
		List<PermissionMenuDTO> list = permissionMenuDAO.queryByObject(queryPermissionMenuDTO);
		List<String> existsList = new ArrayList<String>();
		
		for(PermissionMenuDTO permissionMenuDTO:list){
			if(!ArrayUtils.contains(ownMenus, permissionMenuDTO.getMenuId())){
				permissionMenuDAO.remove(permissionMenuDTO);
			}else{
				existsList.add(permissionMenuDTO.getMenuId());
			}
		}
		List<PermissionMenuDTO> insertList = new ArrayList<PermissionMenuDTO>();
		for(String menuId:ownMenus){
			if(!existsList.contains(menuId)){
				PermissionMenuDTO insertPermissionMenuDTO = new PermissionMenuDTO();
				insertPermissionMenuDTO.setMenuId(menuId);
				insertPermissionMenuDTO.setPermissionId(permissionId);
				insertList.add(insertPermissionMenuDTO);
			}
		}
		if(insertList.size()!=0){
			permissionMenuDAO.createOfBatch(insertList);
		}
		return true;
	}
	
}
